// pages/index/index.js
import * as echarts from '../../ec-canvas/echarts';
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    chartType: 0,
    menuH: '32px',
    safeTop: `40px`,
    ec: '',
    date: '',
    show: false,
    chartShow: true,
    starttime: '',
    endtime: '',
    info: '',
    xAxis: '',
    price: '',
    count: '',
    series: '',
    start: '',
    end: '',
    minDate: new Date(2023, 0, 1).getTime(),
    maxDate: new Date().getTime(),
    token: wx.getStorageSync('token'),
    config: ''
  },
  order(){
    if (!wx.getStorageSync('token')) {
      util.skip('/service/login/login')
      retturn
    }
    wx.requestSubscribeMessage({
      tmplIds: [app.globalData.templateconfig.shop_order_template,app.globalData.templateconfig.shop_finish_template,app.globalData.templateconfig.shop_sales_template],
      success :(res) =>{ 
        console.log(res)
      },
      fail :(e) =>{ 
        console.log(e)
      },
      complete :(res) =>{ 
        wx.switchTab({
          url: '/pages/order/order'
        })
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      safeTop: `${app.globalData.safeTop}px`,
      menuH: `${app.globalData.menuH}px`,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      token: wx.getStorageSync('token')
    })
    this.getInfo()
  },
  onLoadConfig(option){
    this.setData({
      config: app.globalData.config
    })
  },
  timeShow() {
    this.setData({
      show: true,
      chartShow: false
    })
  },

  onDisplay() {
    this.setData({
      show: true,
      chartShow: false
    });
  },
  onClose() {
    this.setData({
      show: false,
      chartShow: true
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirm(event) {

    const [start, end] = event.detail;
    let starttime = new Date(start.toLocaleDateString()).getTime();
    let endtime = new Date(end.toLocaleDateString()).getTime();
    this.setData({
      show: false,
  
      starttime: starttime / 1000,
      endtime: endtime / 1000,
      date: `${util.formatTime(starttime,'yyyy/mm/dd')} - ${util.formatTime(endtime,'yyyy/mm/dd')}`,
    });

    this.getInfo()
  },
  //核销
  tosweep() {
    util.authSkip('/service/sweep/sweep')
  },
  // 平台公告
  toNotice() {
    wx.navigateTo({
      url: '/service/notice/notice'
    })
  },
  cutChart(e) {
    this.setData({
      chartType: e.currentTarget.dataset.type,
      chartShow: false,
    })
    if (this.data.chartType == 1) {
      this.setData({
        series: this.data.count,
        chartShow: true,
        ec: {
          onInit: this.initChart
        }
      })
    } else {
      this.setData({
        series: this.data.price,
        chartShow: true,
        ec: {
          onInit: this.initChart
        }
      })
    }
  },
  initChart(canvas, width, height) {
    const chart = echarts.init(canvas, null, {
      width: width,
      height: height
    });
    canvas.setChart(chart);

    var option = {
      xAxis: {
        type: 'category',
        data: this.data.xAxis
      },
      tooltip: {
        show: true,
        trigger: 'axis'
      },
      yAxis: {
        type: 'value',
        axisLabel: {
          interval: 0
        }
      },
      grid:{
        left:0,
        right:0,
        top:10,
        bottom:32,
        containLabel:true
      },
      series: [{
        data: this.data.series,
        type: 'line',
        itemStyle: {
          normal: {
            color: '#671CF7',
            lineStyle: {
              color: '#671CF7'
            }
          }
        },
      }]
    };
    chart.setOption(option);
    return chart;
  },
  getInfo() {
    
    if (!wx.getStorageSync('token')) return
    this.setData({
      chartShow: false
    })
    let data = {};
    if (this.data.starttime) {
      data['starttime'] = this.data.starttime
      data['endtime'] = this.data.endtime
    }
    http.get('shop/shophome', data, !this.data.info).then(res => {
      let xAxis = res.data.form.map(value => {
        return value.date
      })
      let price = res.data.form.map(value => {
        return value.price
      })
      let count = res.data.form.map(value => {
        return value.count
      })
      this.setData({
        info: res.data,
        xAxis: xAxis,
        price: price,
        count: count,
        date: `${this.formatDate(res.data.form[0].date)} - ${this.formatDate(res.data.form[res.data.form.length-1].date)}`,
      })
      if (this.data.chartType == 1) {
        this.setData({
          series: this.data.count,
          chartShow: true,
          ec: {
            onInit: this.initChart
          }
        })
      } else {
        this.setData({
          series: this.data.price,
          chartShow: true,
          ec: {
            onInit: this.initChart
          }
        })
      }

    })
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {}
  }
})